define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data('area',["100%",'100%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'purchase/allot/index' + location.search,
                    add_url: 'purchase/allot/add',
                    // edit_url: 'purchase/allot/edit',
                    // del_url: 'purchase/allot/del',
                    multi_url: 'purchase/allot/multi',
                    table: 'purchase_allot',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 7) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'allot_id'},
                        {field: 'number', title: __('number'),operate: false},
                        {field: 'source_storehouse_id', title: __('Source_storehouse_id'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=7")},
                        {field: 'storehouse_id', title: __('storehouse_id'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=7")},
                        // {field: 'admin_id', title: __('Admin_id')},
                        // {field: 'group_id', title: __('Group_id')},
                        {field: 'operator', title: __('Operator'),operate: 'LIKE'},
                        {field: 'company', title: __('Company'),operate: 'LIKE'},
                        {field: 'goods_num', title: __('goods_num'),operate:false},
                        {field: 'createtime', title: __('createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('status'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=6")},
                        {field: 'explain', title: __('explain'),operate: false},
                        {
                            field: 'goods_name', title: __('商品名称'), visible: false
                        },
                        // {field: 'allfare', title: __('Allfare')},
                        // {field: 'actual_cost', title: __('Actual_cost'), operate:'BETWEEN'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'examine1',
                                    title: __('待确认'),
                                    classname: 'btn btn-xs btn-default  btn-click editstatus',
                                    icon: 'fa fa-question',
                                    hidden:function(row){
                                        // console.log(row)
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                        if(row.astore_id > 0 &&  row['astore_id'] != row.ysource_storehouse_id){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var allot_id = $(this).parents('tr').find('.allot_id').html()
                                        var url = Config.this_url+'/purchase/allot/detail?ids='+allot_id
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "换货详情",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                        // var allot_id =$(this).parents('tr').find('.allot_id').html()
                                        // layer.confirm('是否通过该申请单？', {
                                        //     btn: ['确定','取消'] //按钮
                                        // }, function(){
                                        //     var url = Config.this_url+'/purchase/allot/examine1?ids='+allot_id
                                        //     $.post(url,{},function (res) {
                                        //         layer.msg(res.msg)
                                        //         if(res.code == 1){
                                        //             $('.btn-refresh').click()
                                        //         }
                                        //     })
                                        // });

                                    }},
                                {
                                    name: 'examine2',
                                    title: __('待发货'),
                                    classname: 'btn btn-xs btn-primary  btn-click editstatus',
                                    icon: 'fa fa-twitter-square ',
                                    hidden:function(row){
                                        if(row.ystatus != 2){
                                            return true;
                                        }
                                        if(row.astore_id > 0 &&  row['astore_id'] != row.ysource_storehouse_id){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var allot_id =$(this).parents('tr').find('.allot_id').html()
                                        var url = Config.this_url+'/purchase/allot/examine2?ids='+allot_id;
                                         layer.open({
                                             type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                             area: ['80%', '90%'],
                                             title: "出库",
                                             content: url,
                                             // content: Config.this_url+'/purchase/requisition',
                                         })

                                    }},
                                {
                                    name: 'examine3',
                                    title: __('待入库'),
                                    classname: 'btn btn-xs btn-success  btn-click editstatus',
                                    icon: 'fa fa-long-arrow-down',
                                    hidden:function(row){
                                        if(row.ystatus != 3){
                                            return true;
                                        }
                                        if(row.astore_id > 0 &&  row['astore_id'] != row.ystorehouse_id){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var allot_id =$(this).parents('tr').find('.allot_id').html()
                                        var url = Config.this_url+'/purchase/allot/examine3?ids='+allot_id;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "确定入库",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })

                                    }},
                                {
                                    name: 'examine4',
                                    title: __('入库审核'),
                                    classname: 'btn btn-xs btn-info  btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(row){
                                        if(row.ystatus != 4){
                                            return true;
                                        }
                                        if(row.astore_id > 0 &&  row['astore_id'] != row.ystorehouse_id){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var allot_id =$(this).parents('tr').find('.allot_id').html()
                                        var url = Config.this_url+'/purchase/allot/examine4?ids='+allot_id;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "确定入库",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'printing',
                                    title: __('打印'),
                                    classname: 'btn btn-xs btn-warning btn-click editstatus',
                                    icon: 'fa fa-print',
                                    click: function(data){
                                        var allot_id =$(this).parents('tr').find('.allot_id').html()
                                        window.open(Config.this_url+'/purchase/allot/printing?ids='+allot_id)
                                    }},
                                // {
                                //     name: 'examine5',
                                //     title: __('中止'),
                                //     classname: 'btn btn-xs btn-danger  btn-click editstatus',
                                //     icon: 'fa fa-times',
                                //     hidden:function(row){
                                //         if(row.ystatus != 1){
                                //             return true;
                                //         }
                                //     },
                                //     click: function(data){
                                //         var allot_id =$(this).parents('tr').find('.allot_id').html()
                                //         layer.confirm('是否驳回该申请单？', {
                                //             btn: ['确定','取消'] //按钮
                                //         }, function(){
                                //             var url = Config.this_url+'/purchase/allot/examine5?ids='+allot_id;
                                //             $.post(url,{status:2},function (res) {
                                //                 layer.msg(res.msg)
                                //                 if(res.code == 1){
                                //                     $('.btn-refresh').click()
                                //                 }
                                //             })
                                //         });
                                //
                                //     }},
                                // {
                                //     name: 'checkout',
                                //     title: __('出库'),
                                //     classname: 'btn btn-xs btn-warning btn-click editstatus',
                                //     icon: 'fa fa-level-down',
                                //     hidden:function(row){
                                //         if(row.ystatus < 2 || row.ystatus >3){
                                //             return true;
                                //         }
                                //     },
                                //     click: function(data){
                                //         var sid =$(this).parents('tr').find('.sid').html()
                                //         var url = Config.this_url+'/sales/checkout/add?slip_id='+sid
                                //         layer.open({
                                //             type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                //             area: ['80%', '90%'],
                                //             title: "出库单",
                                //             content: url,
                                //             // content: Config.this_url+'/purchase/requisition',
                                //         })
                                //     }},
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true)
                                // $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                // console.log(row)
                                if(row['ystatus'] != 1){
                                    // $(table).data("operate-edit", null);// 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }}
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var allot_id = $(this).parents('tr').find('.allot_id').html()
                var url = Config.this_url+'/purchase/allot/detail?ids='+allot_id
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['100%', '100%'],
                    title: "换货详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".btn-add").click(function () {
                if($('#c-source_storehouse_id').val()== 0 ){
                    layer.msg('未选中出库仓库')
                    return false;
                }
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: 'seach?storehouse_id='+$('#c-source_storehouse_id').val(),
                })
            })
            $(document).on('change','input[name="source_storehouse_id"]',function () {
                house_id = $(this).val()
                $('.connnn').find('tr').each(function () {
                    $.post(Config.this_url+'/sales/slip/getgood',{ids:$(this).find('.gid').val(),house_id:house_id},function (res) {
                        if(res != null){
                                $('.gids'+res.id).parents('tr').find('.stock').html(res.stock)
                                $('.gids'+res.id).parents('tr').find('.xstock').html(res.xstock)
                                // console.log(22222222)
                        }else{
                            layer.msg('数据异常')
                            return false;
                        }

                    })
                })
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        examine2: function () {
            Controller.api.bindevent();
        },
        examine3: function () {
            Controller.api.bindevent();
        },
        examine4: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/purchase/allot/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })

            $(table).on('dblclick','tr',function () {
                var gid = $(this).find('.gid').val()
                var purl = Config.this_url+'/goods/goods/stocklist?ids='+gid;
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "库存明细",
                    content: purl,
                    // content: Config.this_url+'/goods/lists?ids='+gid,
                })
            })

            Controller.api.bindevent();
        },
        seach: function () {
            Controller.api.bindevent();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/specs',
                    table: '',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false,class:'sid'},
                        {field: 'title', title: __('title'),  operate: false},
                        {field: 'place_origin', title: __('产地'), formatter: Table.api.formatter.search},
                        {field: 'number', title: __('number')},
                        {field: 'unit', title: __('单位'), align: 'left'},
                        {field: 'specs', title: __('规格')},
                        {field: 'stock', title: __('库存')},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        $(this).attr('style','display:none')
                                        parent.getrequisition(sid);
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){

                var sid =$(this).parents('tr').find('.sid').html()
                parent.getrequisition(sid);
                var index = parent.Layer.getFrameIndex(window.name);
                parent.Layer.close(index);

            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});